package top.hkyzf.study.leetcode.string;

import org.junit.Test;

import java.util.*;

/**
 * @author 朱峰
 * @date 2022-1-30 16:38
 */
public class 两句话中的不常见单词_884 {
    public String[] uncommonFromSentences(String s1, String s2) {
        String[] strs1 = s1.split(" ");
        String[] strs2 = s2.split(" ");
        String[] result = new String[strs1.length + strs2.length];
        Map<String, Integer> map = new HashMap<>();
        for (String s : strs1) {
            map.put(s, map.getOrDefault(s, 0) + 1);
        }
        for (String s : strs2) {
            map.put(s, map.getOrDefault(s, 0) + 1);
        }
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            if (entry.getValue() == 1) {
                result[index] = entry.getKey();
                index++;
            }
        }
        String[] dest = new String[index];
        System.arraycopy(result, 0, dest, 0, index);
        return dest;
    }

    @Test
    public void testUncommonFromSentences () {
        String s1 = "this apple is sweet";
        String s2 = "this apple is sour";
        String[] sentences = uncommonFromSentences(s1, s2);
        System.out.println(Arrays.toString(sentences));
    }
}
